﻿//
var columns =
            [
                 { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                  { display: 'BillState', name: 'BillState', type: 'text', hide: 1 },
                { display: '入库单号', name: 'ProductInBillNo', mintWidth: 80, width: 120 }, //, type: 'int', mintWidth: 40, width: 100
                {display: '入库时间', name: 'ProductInDate', mintWidth: 180, width: 180, type: 'selfDate' },
                { display: 'BillState', name: 'BillState', hide: 1 },
                {display: '单据状态', name: 'BillStateName', mintWidth: 80, width: 120 },
                // { display: '是否结算', name: 'SettleState', mintWidth: 100, width: 140 },
                { display: '仓库名称', name: 'StorehouseName', mintWidth: 80, width: 140 },
                { display: '入库人', name: 'EmployeeName', mintWidth: 80, width: 120 },
                { display: '源组织机构名称', name: 'SourceOrgName', mintWidth: 80, width: 120 },
                //{ display: '送货人名称', name: 'SenderName', mintWidth: 150, width: 200 },
                 { display: '备注', name: 'Remarks', mintWidth: 80, width: 120 }
             ];

var manager;
//init controls
var orgCode = "";//组织机构代码
$(document).ready(function () {
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    getOrgCode();
    initligerGrid();
    initDataControls();
});
function getOrgCode() {
    var $obj = $("#HidOrgCode");
    if ($obj != null && $obj != undefined) {
        if ($obj.val() != undefined && $obj.val() != null && $.trim($obj.val()).length > 0) {
            orgCode = $obj.val();
        }
    }
}
function initligerGrid() {
    
    manager = null;
    manager = $("#ProductInBillGrid").ligerGrid({
        columns: columns,
        title: '入库单管理',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: '99%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                   
                                    { text: '增加', click: dialogAdd, icon: 'add' },
                                    { line: true },
                                    { text: '修改', click: modifyOperation, icon: 'modify' },
                                    { line: true },
//                                    { text: '提交', click: confirmOperation, icon: 'modify' },
//                                    { line: true },
                                    {text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' },
                                    { line: true },
                                    { text: '查看', click: dialogDetailView, img: '../Content/images/Search.png' }
                                    
                                    ]
        },
        parms: { orgCode: $("#HidOrgCode").val(), billNo: $("#billNo").val(),  fhName: $("#fhName").val(), StartDate: $("#StartDate").val(), EndDate: $("#EndDate").val() },
        url: "../ProductIn/GetJsonOfProductInBills?rand=" + Math.random(),
        onDblClickRow: function (data, rowindex, rowobj) { dialogView(data.ID); }
    });
}

//detail
function dialogDetailView() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择要编辑的行!'); return; }
    var billId = row["ID"].toString();
    dialogView(billId);
 }
function dialogView(billId) {
    $.ligerDialog.open({ url: "../ProductIn/Detail?id=" + billId, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false });
}

//add
function dialogAdd() {
    $.ligerDialog.open({ url: "../ProductIn/Add?orgCode=" + $("#HidOrgCode").val(), height: getDialogHeight(), width: getDialogWidth(), 
    isResize: true, isHidden: false
    });
}


//delete
function deleteOperation() {
    var row = manager.getSelectedRow();
    if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    $.ligerDialog.confirm('确定要删除吗？', function (yes) {
        if (yes) {
            if (row["BillState"] == 1) { $.ligerDialog.success('已提交，不能删除！'); return; }
            $.get("../ProductIn/Delete?billId=" + row["ID"].toString() + "&ids=all&rand=" + Math.random(), {}, function (data) {
                if (data != undefined && data == "9") {
                    initligerGrid();
                }
            });
        }
    });
}
//update 
function modifyOperation() {
    var row = manager.getSelectedRow();
    if (!row) {
        $.ligerDialog.success('请选择要编辑的行!'); return;
    }
    if (row["BillState"].toString() == "1") { $.ligerDialog.success('已提交，不能修改!'); return; }
    $.ligerDialog.open({ url: "../ProductIn/Add?orgCode=" + $("#HidOrgCode").val()+"&id=" + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
    });
}

//confirm
//function confirmOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) { $.ligerDialog.success('请选择需要提交行！'); return; }
//    if (row["BillState"] == 1) { $.ligerDialog.success('已提交，不能重复提交！'); return; }
//    $.ligerDialog.confirm('确定要提交吗？', function (yes) {
//        if (yes) {
//            $.get("../ProductIn/Confirm?billId=" + row["ID"].toString(), {}, function (data) {
//                if (data != undefined && data == "9") {
//                    initligerGrid();
//                }
//            });
//        }
//    });
//}



//初始化时间
function initDataControls() {
    $("#StartDate").ligerDateEditor({ showTime: true, format: "yyyy-MM-dd", labelWidth: 140, labelAlign: 'left' });
    $("#EndDate").ligerDateEditor({ showTime: true, format: "yyyy-MM-dd", labelWidth: 140, labelAlign: 'left' });
}